package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.entity.travel.TravelGain;
import com.yanqu.road.entity.travel.TravelResult;
import com.yanqu.road.pb.travel.TravelProto;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.Map;

public class TravelPb {

	public static TravelProto.TravelTempMsg.Builder parseTravelTempMsg(UserTravel userTravel) {
		if(null == userTravel){
			return null;
		}
		TravelProto.TravelTempMsg.Builder msg = TravelProto.TravelTempMsg.newBuilder();
		msg.setPower(userTravel.getPower());
		msg.setLastRestoreTime(userTravel.getLastRestoreTime());
		msg.addAllEventIdList(userTravel.getEventIdList());
		return msg;
	}

	public static TravelProto.TravelPowerMsg.Builder parseTravelPowerMsg(UserTravel userTravel){
		if(null == userTravel){
			return null;
		}
		TravelProto.TravelPowerMsg.Builder msg = TravelProto.TravelPowerMsg.newBuilder();
		msg.setPower(userTravel.getPower());
		msg.setLastRestoreTime(userTravel.getLastRestoreTime());
		return msg;
	}

	/**
	 * 构造游历返回PB
	 * @param travelResult
	 * @param userTravel
	 * @param preEarnSpeed
	 * @param nextEarnSpeed
	 * @return
	 */
	public static TravelProto.TravelGoRespMsg.Builder createTravelGoRespMsg(TravelResult travelResult, UserTravel userTravel, String preEarnSpeed, String nextEarnSpeed) {
		TravelProto.TravelGoRespMsg.Builder respMsg = TravelProto.TravelGoRespMsg.newBuilder();
		respMsg.setRet(travelResult.getRet());
		if(0 == travelResult.getRet()) {
			for (TravelGain travelGain : travelResult.getTravelGainList()) {
				respMsg.addEventIdList(travelGain.getEventId());
			}
		}else {
			if(null != userTravel) {
				respMsg.addAllEventIdList(userTravel.getEventIdList());
			}
		}
		for (TravelGain gain : travelResult.getTravelGainList()) {
			//id写死
			if(gain.getRet() == 0 && gain.getEventId() == 2000005){
				Property property = new Property();
				for (Map.Entry<Integer, BigInteger> entry : gain.getReward().getGoods().entrySet()) {
					if(entry.getKey() == 101006){
						continue;
					}
					property.addProperty(entry.getKey(), entry.getValue());
				}
				respMsg.addReward(PropertyHelper.parsePropertyToString(property));
			}
		}

		if(null != userTravel) {
			respMsg.setCityId(userTravel.getCityId());
		}
		respMsg.setPreEarnSpeed(preEarnSpeed);
		respMsg.setNextEarnSpeed(nextEarnSpeed);
		return respMsg;
	}
}
